/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DateManager;

import JavaClass.ActionAll;
import Main.ExpoManagerMain;
import check.check;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mai Lan
 */
public class DateAction {
      public static  void loadDate(JTable tbdate){

                 Vector datecp = new Vector();
                    Vector datefeil = new Vector();
                     datecp.clear();
                    datecp.add("ID Date");
                    datecp.add("IDEx");

                    datecp.add("SendDate");
                    datecp.add("ReturnDate");
                    datecp.add("LastChangeDate");
                    datecp.add("PaidDate");
                    datecp.add("Status");

                   datefeil.clear();
                    datefeil.add("IdDate");
                    datefeil.add("IDEX");

                    datefeil.add("SendDate");
                    datefeil.add("ReturnDate");
                    datefeil.add("LastChangeDate");
                    datefeil.add("PaidDate");
                    datefeil.add("Status");
                  ActionAll.LoadInfor(datecp, tbdate, datefeil, "ImportantDate");




    }
//     public static  void loadDate(JTable tbdate){
//
//                 Vector datecp = new Vector();
//                    Vector datefeil = new Vector();
//                     datecp.clear();
//                    datecp.add("ID Date");
//                    datecp.add("IDEx");
//                     datecp.add("NameEx");
//                    datecp.add("SendDate");
//                    datecp.add("ReturnDate");
//                    datecp.add("LastChangeDate");
//                    datecp.add("PaidDate");
//                    datecp.add("Status");
//
//                   datefeil.clear();
//                    datefeil.add("ImportantDate.IdDate");
//                    datefeil.add("ImportantDate.IDEX");
//                    datefeil.add("Exhibitor.NameEx");
//                    datefeil.add("ImportantDate.SendDate");
//                    datefeil.add("ImportantDate.ReturnDate");
//                    datefeil.add("ImportantDate.LastChangeDate");
//                    datefeil.add("ImportantDate.PaidDate");
//                    datefeil.add("ImportantDate.Status");
//                  ActionAll.LoadInfor(datecp, tbdate, datefeil, "ImportantDate","Exhibitor","IdDate","ID");
//
//
//
//
//    }
       public static  void insertDate(JTable tbdate,JTextField txtsenddate,JComboBox cobNameEx,JTextField txtReturnDate,JTextField txtPaidDate,JTextField txtxLastchangeDate,JTextArea teaStatus ){
      
       
            try {
                        String sqlEx="select * from Exhibitor where NameEx= '"+(String)cobNameEx.getSelectedItem()+"'";
                        Statement st=ExpoManagerMain.conn.createStatement();
                        ResultSet rs=st.executeQuery(sqlEx);
                        String sql = "Insert into ImportantDate (IDEX,SendDate,ReturnDate," +
                        "LastChangeDate,PaidDate,Status) values(?,?,?,?,?,?)";
                        PreparedStatement ps = ExpoManagerMain.conn.prepareStatement(sql);
                        if(rs.next()){
                        ps.setString(1, rs.getString("ID"));
                        System.out.println((String)cobNameEx.getSelectedItem());
                        ps.setString(2, txtsenddate.getText().trim());
                        ps.setString(3, txtReturnDate.getText().trim());
                        ps.setString(4, txtPaidDate.getText().trim());
                        ps.setString(5, txtxLastchangeDate.getText().trim() );
                        ps.setString(6, teaStatus.getText().trim());
                        ps.executeUpdate();
                      
                        DateAction.loadDate(tbdate);
                        cobNameEx.removeAllItems();
                         check.ValuesFiel(cobNameEx,"Exhibitor","NameEx");
                        }

                    } catch (SQLException ex) {
                        Logger.getLogger(DateManager.class.getName()).log(Level.SEVERE, null, ex);
                    }
             
        

    }
       public static void update(JTable tbdate,JTextField txtsenddate,JComboBox cobNameEx,JTextField txtReturnDate,JTextField txtPaidDate,JTextField txtxLastchangeDate,JTextArea teaStatus ){

        if(tbdate.isCellSelected(tbdate.getSelectedRow(),tbdate.getSelectedColumn())==false){
                JOptionPane.showMessageDialog(null, "Please choice information want to update on list");
                return;
        }else{
            try {

                int n = tbdate.getSelectedRow();
                String Id = (String) tbdate.getValueAt(n, 0);
                String IDex=(String)tbdate.getValueAt(n, 1);
                
                String sqlEx="select * from Exhibitor where NameEx= '"+(String)cobNameEx.getSelectedItem()+"'";
                Statement stEX=ExpoManagerMain.conn.createStatement();
                ResultSet rs=stEX.executeQuery(sqlEx);
                if(rs.next()){
                System.out.println(rs.getString("ID"));
                String sql = "Update ImportantDate set SendDate='" + txtsenddate.getText().trim()
                        + "',ReturnDate='" + txtReturnDate.getText().trim() + "',LastChangeDate='"
                        + txtxLastchangeDate.getText().trim() + "',PaidDate='" + txtPaidDate.getText().trim()
                        + "',Status='" + teaStatus.getText().trim() + "', IDEX="+rs.getString("ID")+" where IdDate=" + Id;

                Statement st = ExpoManagerMain.conn.createStatement();
                st.executeUpdate(sql);
               DateAction.loadDate(tbdate);
                cobNameEx.removeAllItems();
                check.ValuesFiel(cobNameEx,"Exhibitor","NameEx");
                }
            } catch (SQLException ex) {
                Logger.getLogger(DateManager.class.getName()).log(Level.SEVERE, null, ex);
            }



        }


    }
       public  static  void deleteDate(JTable tbdate) throws SQLException{
        ActionAll.deleteInfor(tbdate, "ImportantDate", "IdDate");
        
        DateAction.loadDate(tbdate);
               
            

    }
}
